using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class ClientAnnal
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("ClientAnnalID", "HL_ClientAnnal");
		}
		public bool Exists(int ClientAnnalID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_ClientAnnal");
			stringBuilder.Append(" where ClientAnnalID=@ClientAnnalID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientAnnalID", SqlDbType.Int, 4)
			};
			array[0].Value = ClientAnnalID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.ClientAnnal model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_ClientAnnal(");
			stringBuilder.Append("ClientID,RelationType,RelationTime,Contacter,AnnalContent,AnnalTime,Inputer)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@ClientID,@RelationType,@RelationTime,@Contacter,@AnnalContent,@AnnalTime,@Inputer)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4),
				new SqlParameter("@RelationType", SqlDbType.NVarChar, 30),
				new SqlParameter("@RelationTime", SqlDbType.DateTime),
				new SqlParameter("@Contacter", SqlDbType.NVarChar, 20),
				new SqlParameter("@AnnalContent", SqlDbType.NVarChar, 1000),
				new SqlParameter("@AnnalTime", SqlDbType.DateTime),
				new SqlParameter("@Inputer", SqlDbType.NVarChar, 50)
			};
			array[0].Value = model.ClientID;
			array[1].Value = model.RelationType;
			array[2].Value = model.RelationTime;
			array[3].Value = model.Contacter;
			array[4].Value = model.AnnalContent;
			array[5].Value = model.AnnalTime;
			array[6].Value = model.Inputer;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.ClientAnnal model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_ClientAnnal set ");
			stringBuilder.Append("ClientID=@ClientID");
			if (!string.IsNullOrEmpty(model.RelationType))
			{
				stringBuilder.Append(",RelationType='" + model.RelationType + "'");
			}
			if (model.RelationTime.HasValue)
			{
				stringBuilder.Append(",RelationTime='" + model.RelationTime + "'");
			}
			if (!string.IsNullOrEmpty(model.Contacter))
			{
				stringBuilder.Append(",Contacter='" + model.Contacter + "'");
			}
			if (!string.IsNullOrEmpty(model.AnnalContent))
			{
				stringBuilder.Append(",AnnalContent='" + model.AnnalContent + "'");
			}
			if (model.AnnalTime.HasValue)
			{
				stringBuilder.Append(",AnnalTime='" + model.AnnalTime + "'");
			}
			if (!string.IsNullOrEmpty(model.Inputer))
			{
				stringBuilder.Append(",Inputer='" + model.Inputer + "'");
			}
			stringBuilder.Append(" where ClientAnnalID=@ClientAnnalID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4),
				new SqlParameter("@ClientAnnalID", SqlDbType.Int, 4)
			};
			array[0].Value = model.ClientID;
			array[1].Value = model.ClientAnnalID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int ClientAnnalID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientAnnal ");
			stringBuilder.Append(" where ClientAnnalID=@ClientAnnalID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientAnnalID", SqlDbType.Int, 4)
			};
			array[0].Value = ClientAnnalID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string ClientAnnalIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientAnnal ");
			stringBuilder.Append(" where ClientAnnalID in (" + ClientAnnalIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.ClientAnnal GetModel(int ClientAnnalID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 ClientAnnalID,ClientID,RelationType,RelationTime,Contacter,AnnalContent,AnnalTime,Inputer from HL_ClientAnnal ");
			stringBuilder.Append(" where ClientAnnalID=@ClientAnnalID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientAnnalID", SqlDbType.Int, 4)
			};
			array[0].Value = ClientAnnalID;
			HKB.Data.Model.ClientAnnal clientAnnal = new HKB.Data.Model.ClientAnnal();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.ClientAnnal result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.ClientAnnal DataRowToModel(DataRow row)
		{
			HKB.Data.Model.ClientAnnal clientAnnal = new HKB.Data.Model.ClientAnnal();
			if (row != null)
			{
				if (row["ClientAnnalID"] != null && row["ClientAnnalID"].ToString() != "")
				{
					clientAnnal.ClientAnnalID = int.Parse(row["ClientAnnalID"].ToString());
				}
				if (row["ClientID"] != null && row["ClientID"].ToString() != "")
				{
					clientAnnal.ClientID = int.Parse(row["ClientID"].ToString());
				}
				if (row["RelationType"] != null)
				{
					clientAnnal.RelationType = row["RelationType"].ToString();
				}
				if (row["RelationTime"] != null && row["RelationTime"].ToString() != "")
				{
					clientAnnal.RelationTime = new System.DateTime?(System.DateTime.Parse(row["RelationTime"].ToString()));
				}
				if (row["Contacter"] != null)
				{
					clientAnnal.Contacter = row["Contacter"].ToString();
				}
				if (row["AnnalContent"] != null)
				{
					clientAnnal.AnnalContent = row["AnnalContent"].ToString();
				}
				if (row["AnnalTime"] != null && row["AnnalTime"].ToString() != "")
				{
					clientAnnal.AnnalTime = new System.DateTime?(System.DateTime.Parse(row["AnnalTime"].ToString()));
				}
				if (row["Inputer"] != null)
				{
					clientAnnal.Inputer = row["Inputer"].ToString();
				}
			}
			return clientAnnal;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ClientAnnalID,ClientID,RelationType,RelationTime,Contacter,AnnalContent,AnnalTime,Inputer ");
			stringBuilder.Append(" FROM HL_ClientAnnal ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" ClientAnnalID,ClientID,RelationType,RelationTime,Contacter,AnnalContent,AnnalTime,Inputer ");
			stringBuilder.Append(" FROM HL_ClientAnnal ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_ClientAnnal ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.ClientAnnalID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_ClientAnnal T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public HKB.Data.Model.ClientAnnal GetAnnalItem(string whereStr)
		{
			string text = "select * from HL_ClientAnnal where " + whereStr;
			HKB.Data.Model.ClientAnnal result = null;
			DataSet dataSet = Conn.Query(text.ToString(), new IDbDataParameter[0]);
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			return result;
		}
		public HKB.Data.Model.ClientAnnal GetClientAnnalByClientID(int ClientID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HL_ClientAnnal ");
			stringBuilder.Append(" where ClientID=" + ClientID);
			stringBuilder.Append(" order by RelationTime desc");
			HKB.Data.Model.ClientAnnal result = null;
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			return result;
		}
	}
}
